function Auth() {
	this.mask = $('.mask-wrapper');
	this.scroll_wrapper = $('.scroll-wrapper');
	this.signin_btn = $('#signin-btn');
	this.signup_btn = $('#signup-btn');
}

Auth.prototype.listenSigninBtnEvent = function () {
	var self = this;
	this.signin_btn.click(function () {
		document.body.style.overflow = 'hidden';
		self.mask.show();
		self.scroll_wrapper.animate({'left': '0'});
	})
};

Auth.prototype.listenSignupBtnEvent = function () {
	var self = this;
	this.signup_btn.click(function () {
		document.body.style.overflow = 'hidden';
		self.mask.show();
		self.scroll_wrapper.animate({'left': '-400px'});
	})
};

Auth.prototype.listenCloseBtnEvent = function () {
	var self = this;
	$('.close-btn').click(function () {
		document.body.style.overflow = 'auto';
		self.mask.hide();
		$('input[name="password"]').val('');
	})
};

Auth.prototype.listenSwitchBtnEvent = function () {
	var self = this;
	$('.switch').click(function () {
		var current_left = self.scroll_wrapper.css('left');
		current_left = parseInt(current_left);
		if (current_left >= 0) {
			self.scroll_wrapper.animate({left: '-400px'})
		} else {
			self.scroll_wrapper.animate({left: '0'})
		}
	})
};

Auth.prototype.listenSigninAjaxEvent = function () {
    var self = this;
    var signin_group = this.scroll_wrapper.find('.signin-group');
    var emailInput = signin_group.find("input[name='email']");
    var passwordInput = signin_group.find("input[name='password']");
    var rememberInput = signin_group.find("input[name='remember']");
    var btn = signin_group.find(".submit-btn");
    btn.click(function () {
        var email = emailInput.val();
        var password = passwordInput.val();
        var remember = rememberInput.prop('checked');
        myajax.post({
           'url': '/auth/login/',
            'data': {
               'email': email,
                'password': password,
                'remember': remember
            },
            'success': function (result) {
                if (result['code'] === 200) {
                    self.mask.hide();
                    window.location.href = window.location.href;
                }
            }
        });
    })
};

Auth.prototype.listenSignupAjaxEvent = function () {
	var self = this;
	var signup_group = this.scroll_wrapper.find('.signup-group');
    var emailInput = signup_group.find("input[name='email']");
    var passwordInput = signup_group.find("input[name='password']");
    var passwordRepeatInput = signup_group.find("input[name='password_repeat']");
    var usernameInput = signup_group.find('input[name="username"]');
    var imgCaptchaInput = signup_group.find('input[name="img_captcha"]');
	var emailCaptchaInput = signup_group.find('input[name="email_captcha"]');
	var btn = signup_group.find(".submit-btn");
	btn.click(function () {
		var email = emailInput.val();
		var username = usernameInput.val();
		var password = passwordInput.val();
		var passwordRepeat = passwordRepeatInput.val();
		var imgCaptcha = imgCaptchaInput.val();
		var emailCaptcha = emailCaptchaInput.val();
		myajax.post({
			'url': '/auth/register/',
			'data': {
				'email': email,
				'username': username,
				'password': password,
				'password_repeat': passwordRepeat,
				'img_captcha': imgCaptcha,
				'email_captcha': emailCaptcha
			},
			'success': function (result) {
				if (result['code'] === 200) {
					window.location.href = window.location.href;
				}
			}
		});
	})
};

Auth.prototype.listenImageCaptchaEvent = function () {
	var img_captcha = this.scroll_wrapper.find('.image');
	img_captcha.click(function () {
		img_captcha.attr('src','/auth/img_captcha/'+'?random=' + (new Date()).getTime());
	})
};

Auth.prototype.EmailSuccessEvent = function () {
	var self = this;
	var btn = this.scroll_wrapper.find('.email-captcha-btn');
	btn.addClass('disabled');
		btn.unbind('click');
		btn.css({'cursor': 'default'});
		var count = 60;
		var timer = setInterval(function () {
			btn.text('请等待'+count+'秒');
			count--;
			if (count<=0) {
				clearInterval(timer);
				btn.removeClass('disabled');
				btn.text('发送验证码');
				btn.css({'cursor': 'pointer'});
				self.listenEmailSend();
			}
		},1000);
};

Auth.prototype.listenEmailSend = function () {
	var self = this;
	var btn = this.scroll_wrapper.find('.email-captcha-btn');
	var signup = this.scroll_wrapper.find('.signup-group');
	var emailInput = signup.find('input[name="email"]');

	btn.click(function () {
		var email = emailInput.val();
		if (email == '') {
			window.messageBox.showInfo('请输入邮箱！');
			return;
		}
		myajax.post({
			'url': '/auth/email_captcha/',
			'data': {
				'email': email
			},
			'success': function (result) {
				if (result['code'] === 200) {
					window.messageBox.showSuccess('验证码发送成功！');
					self.EmailSuccessEvent();
				} else {
					window.messageBox.showInfo(result['message']);
				}
			}
		})
	})
};

Auth.prototype.run = function () {
	this.listenSigninBtnEvent();
	this.listenSignupBtnEvent();
	this.listenCloseBtnEvent();
	this.listenSwitchBtnEvent();
	this.listenSigninAjaxEvent();
	this.listenSignupAjaxEvent();
	this.listenImageCaptchaEvent();
	this.listenEmailSend();
};

$(function () {
	var auth = new Auth();
	auth.run();
});